/*
 * Stopwords.cpp
 *
 *  Created on: 29/09/2011
 *      Author: diego
 */

#include "Stopwords.h"

Stopwords::Stopwords() {
	this->stpw = new ifstream();
	this->stopwords = new Trie();

	string path = PATH_STOPW;
	this->stpw->open(path.c_str());

	if (!this->stpw->good()) {
		//throw FileBadException("Error al abrir el documento");
		cout << "Error al abrir el documento";
	}

	string word = "";
	char charLeido = 1;
	(*this->stpw) >> noskipws >> charLeido;
	while (!this->stpw->eof()){
		while (!this->stpw->eof() && charLeido != '\n') {
			if (this->charValido(charLeido)){
				word += charLeido;
			}
			(*this->stpw) >> noskipws >> charLeido;
		}

		//this->stpw_list->agregar(word);
		this->stopwords->insertar(word);
		word = "";
		word.clear();
		(*this->stpw) >> noskipws >> charLeido;
	}

	this->stpw->close();
}

bool Stopwords::charValido(char c){
	return ((c <= 57 && c >= 48) || (c <= 122 && c >= 97));
}

void Stopwords::agregarStopword(string termino){
	this->stopwords->insertar(termino);
}

bool Stopwords::isStopword(string word){
	return this->stopwords->buscar(word);
}

void Stopwords::extraerStopwords(){
	Configuracion* conf = Configuracion::getConf(PATH_CONF);

	IteradorIndice* entrada = new IteradorIndice();

	super_reg* reg;
	int cant_terminos = entrada->getCantTerminos();

	// solo extraigo stopswords si el indice tiene mas de 50 mil terminos
	if (cant_terminos > 50000){
		IndexWriter* salida = new IndexWriter("tmp", conf->front_coding());

		//cout << "cant de terminos: " << cant_terminos << endl;

		while(entrada->tieneSiguiente()){
			reg = entrada->proximoRegistro();

			float porcentaje = (reg->frec_t * 1.0) / (cant_terminos * 1.0);
			if (porcentaje > 0.12){
				//cout << reg->termino << ": " << porcentaje << endl;
				this->agregarStopword(reg->termino);
			}else{
				salida->write(reg);
			}

			this->borrar_registro(reg);
			reg = NULL;
		}

		salida->close();

		delete salida;

		string path = conf->path_indice() + "indice.dat";
		remove(path.c_str());

		path = conf->path_indice() + "terminos.dat";
		remove(path.c_str());

		path = conf->path_indice() + "docs.dat";
		remove(path.c_str());

		path = conf->path_indice() + "header.dat";
		remove(path.c_str());

		string old_s, new_s;

		old_s = conf->path_indice() + "tmp_indice.dat";
		new_s = conf->path_indice() + "indice.dat";
		rename(old_s.c_str(), new_s.c_str());

		old_s = conf->path_indice() + "tmp_terminos.dat";
		new_s = conf->path_indice() + "terminos.dat";
		rename(old_s.c_str(), new_s.c_str());

		old_s = conf->path_indice() + "tmp_docs.dat";
		new_s = conf->path_indice() + "docs.dat";
		rename(old_s.c_str(), new_s.c_str());

		old_s = conf->path_indice() + "tmp_header.dat";
		new_s = conf->path_indice() + "header.dat";
		rename(old_s.c_str(), new_s.c_str());

		this->grabarStopwords();
	}

	delete entrada;

}

void Stopwords::grabarStopwords(){

	ofstream* fd = new ofstream(PATH_STOPW, ios::out);
	this->stopwords->grabarArbol(fd);
	fd->close();
	delete fd;
	return;
}

void Stopwords::borrar_registro(super_reg* reg){

	IteradorLista<reg_docs_variable*> it = reg->lista->iterador();
	while (it.tieneSiguiente()){
		delete it.siguiente();
	}

	delete reg->lista;
	delete reg;
}

Stopwords::~Stopwords() {
	//delete this->stpw_list;
	delete this->stopwords;

	delete this->stpw;
}

